using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace HSL2RGB
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            HSL hsl = new HSL();
            hsl.H = 0.340;
            hsl.S = 0.10;
            hsl.L = 0.80;
            double[] color = HSL2RGB(3, 0.88, 0.10); //Color.FromArgb((int)color[0], (int)color[1], (int)color[2]);
            this.BackColor = RGBHSL.HSL_to_RGB(hsl);
        }
        //x=r*cos phi
        //y=r *sin hi
        private double[] HSL2RGB(double H, double S, double L)
        {
            double R,G,B;
            if ( S == 0 )                       //HSL from 0 to 1
            {
               R = L * 255;                     //RGB results from 0 to 255
               G = L * 255;
               B = L * 255;
            }
            else
            {
                double var_1, var_2;
               if ( L < 0.5 ) var_2 = L * ( 1 + S );
               else           var_2 = ( L + S ) - ( S * L );

               var_1 = 2 * L - var_2;

               R = Math.Abs(255 * Hue_2_RGB( var_1, var_2, H + ( 1 / 3 ) ) );
               G = Math.Abs(255 * Hue_2_RGB( var_1, var_2, H ));
               B = Math.Abs(255 * Hue_2_RGB( var_1, var_2, H - ( 1 / 3 ) ));
            }
            double[] color = {R,G,B };
            return color;
        }
        private double Hue_2_RGB(double v1,double v2,double vH )             
        {
           if ( vH < 0 ) vH += 1;
           if ( vH > 1 ) vH -= 1;
           if ( ( 6 * vH ) < 1 ) return ( v1 + ( v2 - v1 ) * 6 * vH );
           if ( ( 2 * vH ) < 1 ) return ( v2 );
           if ( ( 3 * vH ) < 2 ) return ( v1 + ( v2 - v1 ) * ( ( 2 / 3 ) - vH ) * 6 );
           return (v1);
        }
    }
}